#include <my_header.h>

typedef struct share_value_s
{
    int flag;
    pthread_mutex_t mLock;
    pthread_cond_t cond;
}share_value_t;

void * threadFunc(void * arg);

/* usage */
int main(int argc, char *argv[])
{
    ARGS_CHECK(argc,1);
    share_value_t share_value;
    share_value.flag = 0;
    pthread_mutex_init(&share_value.mLock,NULL);
    pthread_cond_init(&share_value.cond,NULL);

    pthread_t tid;
    int ret_create = pthread_create(&tid,NULL,threadFunc,&share_value);
    THREAD_ERROR_CHECK(ret_create,"pthread_create");

    printf("Before A!\n");
    sleep(3);
    printf("After A!\n");
    pthread_mutex_lock(&share_value.mLock);
    share_value.flag = 1;
    pthread_cond_signal(&share_value.cond);
    pthread_mutex_unlock(&share_value.mLock);
    pthread_join(tid,NULL);
    return 0;
}

void * threadFunc(void * arg)
{
    share_value_t *pshare_value = (share_value_t *)arg;
    pthread_mutex_lock(&pshare_value->mLock);
    while(pshare_value->flag == 0)
    {
        pthread_cond_wait(&pshare_value->cond,&pshare_value->mLock);
    }
    printf("Before B!\n");
    sleep(3);
    printf("After B!\n");
    pthread_mutex_unlock(&pshare_value->mLock);
    pthread_exit(NULL);
}

